package com.eatme.web.sys.service.impl;

import com.eatme.web.common.vo.AppResponse;
import com.eatme.web.param.RoleParam;
import com.eatme.web.sys.model.*;
import com.eatme.web.sys.repository.SysRoleRepository;
import com.eatme.web.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    SysRoleRepository sysRoleRepository;


    @Override
    public Object getRoleListByUserId(int userId) {
        return null;
    }

    @Override
    public Object getAll() {
        return null;
    }

    @Override
    public void update(RoleParam param) {

    }

    @Override
    public void save(RoleParam param) {

    }

    @Override
    public Object getUserListByRoleList(List<SysRole> roleList) {
        return null;
    }

    @Override
    public List<SysRole> getRoleListByAclId(int aclId) {
        return null;
    }

    @Override
    public List findList() {
        com.eatme.web.sys.entity.SysRole role = new com.eatme.web.sys.entity.SysRole();
        role.setStatus(1);
        ExampleMatcher matcher = ExampleMatcher.matching();
        Example<com.eatme.web.sys.entity.SysRole> example = Example.of(role,matcher);
        List<com.eatme.web.sys.entity.SysRole> roleList = sysRoleRepository.findAll(example);
        return  roleList;
    }

    @Override
    public com.eatme.web.sys.entity.SysRole getDetail(Integer id) {
        com.eatme.web.sys.entity.SysRole sysRole = sysRoleRepository.findById(id).get();
        return sysRole;
    }

    @Override
    public List<Integer> findResource(Integer id) {
        com.eatme.web.sys.entity.SysRole sysRole = sysRoleRepository.findById(id).get();
        List<Integer> permission = new ArrayList<>();
        sysRole.getSysResources().forEach(item-> {
            permission.add(item.getId());

        });
        return permission;
    }

    @Override
    public AppResponse updateRoleResource(com.eatme.web.sys.entity.SysRole sysRole) {
        sysRole = sysRoleRepository.save(sysRole);
        return AppResponse.ok().data(sysRole);
    }

    @Override
    public AppResponse delete(Integer id) {
        sysRoleRepository.updateSysRoleStatusById(id);
        return AppResponse.ok();
    }

    @Override
    public AppResponse edit(com.eatme.web.sys.entity.SysRole sysRole) {
        com.eatme.web.sys.entity.SysRole existSysRole = sysRoleRepository.findById(sysRole.getId()).get();
        existSysRole.setName(sysRole.getName());
        existSysRole.setDescription(sysRole.getDescription());
        sysRole = sysRoleRepository.save(existSysRole);
        return AppResponse.ok();
    }

    @Override
    public AppResponse add(com.eatme.web.sys.entity.SysRole sysRole) {
        sysRole.setStatus(1);
        sysRole.setType(2);
        sysRole = sysRoleRepository.save(sysRole);
        return AppResponse.ok();
    }

}
